<?php
namespace Admin\Controller;
use Think\Controller;
class UploadsController extends CommonController {

    /*
     * 图片上传接口
     * */
    public function uploadImage(){
        if (!empty($_FILES)) {
            //图片上传设置
            $config = array(
                'maxSize'    =>    3145728,
                'rootPath'	 =>    './',
                'savePath'   =>    '/upload/images/',
                'saveName'   =>    array('uniqid',''),
                'exts'       =>    array('jpg', 'gif', 'png', 'jpeg'),
                'autoSub'    =>    true,//自动使用子目录保存上传文件 默认为true
                'subName'    =>    array('date','Y/md'),
            );
            $upload = new \Think\Upload($config);// 实例化上传类
            $images = $upload->upload();
            if($images){
                $info = $images['file']['savepath'].$images['file']['savename'];
                // 按照原图的比例生成一个最大为150*150的缩略图并保存为thumb.jpg
                $image = new \Think\Image();
                $image->open('.'.$info);
                $image->thumb(200, 200)->save('.'.$images['file']['savepath'].'thumb_'.$images['file']['savename']);
                self::response('200','success', $info);
            }else{
                self::response('400', 'error', $upload->getError());
            }
        }else{
            self::response('400', 'error', '文件不存在');
        }
    }

    /*
     * 附件上传接口
     * */
    public function uploadAttachment(){

        if (!empty($_FILES)) {
            $config = array(
                'maxSize'    =>    100000000,
                'rootPath'	 =>    './',
                'savePath'   =>    '/upload/file/',
                'saveName'   =>    array('uniqid',''),
//                'exts'       =>    array('jpg', 'gif', 'png', 'jpeg'),
                'autoSub'    =>    true,//自动使用子目录保存上传文件 默认为true
                'subName'    =>    array('date', 'Y/md'),
            );
            $upload = new \Think\Upload($config);// 实例化上传类
            $attachment = $upload->upload();
            if($attachment){
                $data = array();
                $data['file_name'] = $attachment['Filedata']['name'];
                $data['file_size'] = $attachment['Filedata']['size'];
                $data['file'] = $attachment['Filedata']['savepath'].$attachment['Filedata']['savename'];
                //返回文件地址和名给JS作回调用
                self::response('200','success', $data);
            }else{
                self::response('400','success', $upload->getError());
            }
        }
    }
}